home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / timed / correct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-23  |  3.0 KB  |  146 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)correct.c    2.4 (Berkeley) 12/23/87";
  15. #endif /* not lint */
  16.  
  17. #include "globals.h"
  18. #include <protocols/timed.h>
  19.  
  20. #ifdef MEASURE
  21. extern FILE *fp;
  22. #endif
  23.  
  24. /* 
  25.  * `correct' sends to the slaves the corrections for their clocks
  26.  */
  27.  
  28. correct(avdelta)
  29. long avdelta;
  30. {
  31.     int i;
  32.     int corr;
  33.     struct timeval adjlocal;
  34.     struct tsp msgs;
  35.     struct timeval mstotvround();
  36.     struct tsp *answer, *acksend();
  37.  
  38. #ifdef MEASURE
  39.     for(i=0; i<slvcount; i++) {
  40.         if (hp[i].delta == HOSTDOWN)
  41.             fprintf(fp, "%s\t", "down");
  42.         else { 
  43.             fprintf(fp, "%d\t", hp[i].delta);
  44.         }
  45.     }
  46.     fprintf(fp, "\n");
  47. #endif
  48.     corr = avdelta - hp[0].delta;
  49.     adjlocal = mstotvround(&corr);
  50.     adjclock(&adjlocal);
  51. #ifdef MEASURE
  52.     fprintf(fp, "%d\t", corr);
  53. #endif
  54.  
  55.     for(i=1; i<slvcount; i++) {
  56.         if (hp[i].delta != HOSTDOWN)  {
  57.             corr = avdelta - hp[i].delta;
  58.             msgs.tsp_time = mstotvround(&corr);
  59.             msgs.tsp_type = (u_char)TSP_ADJTIME;
  60.             (void)strcpy(msgs.tsp_name, hostname);
  61.             answer = acksend(&msgs, &hp[i].addr, hp[i].name,
  62.                 TSP_ACK, (struct netinfo *)NULL);
  63.             if (answer == NULL) {
  64.                 hp[i].delta = HOSTDOWN;
  65. #ifdef MEASURE
  66.                 fprintf(fp, "%s\t", "down");
  67.             } else {
  68.                 fprintf(fp, "%d\t", corr);
  69. #endif
  70.             }
  71.         } else {
  72. #ifdef MEASURE
  73.             fprintf(fp, "%s\t", "down");
  74. #endif
  75.         }
  76.     }
  77. #ifdef MEASURE
  78.     fprintf(fp, "\n");
  79. #endif
  80. }
  81.  
  82. /* 
  83.  * `mstotvround' rounds up the value of the argument to the 
  84.  * nearest multiple of five, and converts it into a timeval 
  85.  */
  86.  
  87. struct timeval mstotvround(x)
  88. int *x;
  89. {
  90.     int temp;
  91.     struct timeval adj;
  92.  
  93.     temp = *x % 5;
  94.     if (temp >= 3)
  95.         *x = *x-temp+5;
  96.     else {
  97.         if (temp <= -3)
  98.             *x = *x - temp -5;
  99.         else 
  100.             *x = *x-temp;
  101.     }
  102.     adj.tv_sec = *x/1000;
  103.     adj.tv_usec = (*x-adj.tv_sec*1000)*1000;
  104.     if (adj.tv_usec < 0) {
  105.         adj.tv_usec += 1000000;
  106.         adj.tv_sec--;
  107.     }
  108.     return(adj);
  109. }
  110.  
  111. adjclock(corr)
  112. struct timeval *corr;
  113. {
  114.     struct timeval now;
  115.  
  116.     if (timerisset(corr)) {
  117.         if (corr->tv_sec < MAXADJ && corr->tv_sec > - MAXADJ) {
  118.             (void)adjtime(corr, (struct timeval *)0);
  119.         } else {
  120.             syslog(LOG_WARNING,
  121.                 "clock correction too large to adjust (%d sec)",
  122.                 corr->tv_sec);
  123.             (void) gettimeofday(&now, (struct timezone *)0);
  124.             timevaladd(&now, corr);
  125.             if (settimeofday(&now, (struct timezone *)0) < 0)
  126.                 syslog(LOG_ERR, "can't set time");
  127.         }
  128.     }
  129. }
  130.  
  131. timevaladd(tv1, tv2)
  132.     register struct timeval *tv1, *tv2;
  133. {
  134.     
  135.     tv1->tv_sec += tv2->tv_sec;
  136.     tv1->tv_usec += tv2->tv_usec;
  137.     if (tv1->tv_usec >= 1000000) {
  138.         tv1->tv_sec++;
  139.         tv1->tv_usec -= 1000000;
  140.     }
  141.     if (tv1->tv_usec < 0) {
  142.         tv1->tv_sec--;
  143.         tv1->tv_usec += 1000000;
  144.     }
  145. }
  146.